<!-- ************************************************************************************************************ -->
<!-- fileName          : DeleteTopguide.jsp 																	  -->
<!-- author            : 马文军 																					  --> 
<!-- createDate        : 2015/01/06 																			  -->
<!-- summary           : admin端  “顶部导航” 删除操作   后台逻辑 																  -->
<!-- modifiedBy        : 马文军																				      -->
<!-- ************************************************************************************************************ -->

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>DeleteTopguide</title>
<style type="text/css">

</style>

</head>
<body>
<%
	//设置请求的字符编码以支持中文
	request.setCharacterEncoding ("utf-8");

	//从请求页面获取表单参数
    String id = request.getParameter("id");
	System.out.println("topguide删除	id="+id);
	String father_id = request.getParameter("father_id");		//返回时参数
   
	//从web.xml文件获取全局参数
	String drv=config.getServletContext().getInitParameter("DBDriver");
	String url=config.getServletContext().getInitParameter("DBURL");
	String uid=config.getServletContext().getInitParameter("DBUser");
	String pwd=config.getServletContext().getInitParameter("DBPass");
	
    //构建SQL字符串和JDBC对象
    
	String strSQL1 = "delete from immie0_immi.topguide WHERE id=? ";
	String strSQL2 = "SELECT id FROM immie0_immi.topguide tg WHERE tg.father_id=?";	 

	Connection conn = null;
	PreparedStatement pstmt = null;	
	ResultSet rs1 = null; //子节点
	ResultSet rs2 = null; //子子节点
	
	try {
		
		//查找    并   删除子节点
		try {
			Class.forName(drv).newInstance();
			conn = DriverManager.getConnection(url,uid,pwd);
			pstmt = conn.prepareStatement(strSQL2);
			//设置SQL语句参数
			pstmt.setString(1, id);
			rs1 = pstmt.executeQuery(); //得到所有子节点
			
			while(rs1.next()) {
				//查找    并   删除子子节点
				try {
					Class.forName(drv).newInstance();
					conn = DriverManager.getConnection(url,uid,pwd);
					pstmt = conn.prepareStatement(strSQL2);
					//设置SQL语句参数
					pstmt.setString(1, rs1.getString(1) );
			
					rs2 = pstmt.executeQuery();	//得到所有子子节点
			
					while(rs2.next()) { 		
						//删除  子子节点
						try {
							Class.forName(drv).newInstance();
							conn = DriverManager.getConnection(url,uid,pwd);
							//删除节点
							pstmt = conn.prepareStatement(strSQL1);
							//设置SQL语句参数
							pstmt.setString(1, rs2.getString(1));
							pstmt.executeUpdate(); 
							
							System.out.println("删除 子子节点	id="+rs2.getString(1));
						}catch (ClassNotFoundException e) {
							e.printStackTrace();
						} catch (InstantiationException e) {
						    e.printStackTrace();
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						} catch (SQLException e) {
							e.printStackTrace();
						} 
						finally {
							if (pstmt != null) {
								pstmt.close();
								pstmt = null;
							}		
							if (conn != null) {
								conn.close();
								conn = null;
							}
						}//删除  子子节点 try 结束
					}	//子子节点 while 结束
				}catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
				    e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				} 
				finally {
					if (rs2 != null) {
						rs2.close();
						rs2 = null;
					}
					if (pstmt != null) {
						pstmt.close();
						pstmt = null;
					}		
					if (conn != null) {
						conn.close();
						conn = null;
					}
				}	//查找子子节点 try 结束
				
				//删除 子节点
				try {
					Class.forName(drv).newInstance();
					conn = DriverManager.getConnection(url,uid,pwd);
					//删除节点
					pstmt = conn.prepareStatement(strSQL1);
					//设置SQL语句参数
					pstmt.setString(1, rs1.getString(1));
					pstmt.executeUpdate();  
					
					System.out.println("删除 子节点	id="+rs1.getString(1));
				}catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
				    e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				} 
				finally {
					if (pstmt != null) {
						pstmt.close();
						pstmt = null;
					}		
					if (conn != null) {
						conn.close();
						conn = null;
					}
				}// 删除子节点     try 结束
			} //子节点 while循环     结束
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
		    e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		finally {
			if (rs1 != null) {
				rs1.close();
				rs1 = null;
			}
			if (rs2 != null) {
				rs2.close();
				rs2 = null;
			}
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}		
			if (conn != null) {
				conn.close();
				conn = null;
			}
		}	//查找子节点 try 结束
		
		Class.forName(drv).newInstance();
		conn = DriverManager.getConnection(url,uid,pwd);
		//删除节点
		pstmt = conn.prepareStatement(strSQL1);
		//设置SQL语句参数
		pstmt.setString(1, id);
		pstmt.executeUpdate();  
		
		System.out.println("删除完成	id="+id);
		
		if(father_id.equals("0")){
			response.sendRedirect("./admin_topguide.jsp");
		}
		else	{
			response.sendRedirect("./EditTopguideFather.do?id="+father_id);
		}
				
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (InstantiationException e) {
	    e.printStackTrace();
	} catch (IllegalAccessException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	} 
	finally {
		if (rs1 != null) {
			rs1.close();
			rs1 = null;
		}
		if (rs2 != null) {
			rs2.close();
			rs2 = null;
		}
		if (pstmt != null) {
			pstmt.close();
			pstmt = null;
		}
		if (conn != null) {
			conn.close();
			conn = null;
		}
	}
%>
</body>
</html>